using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class AccountAoucher
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("AccountAoucherID", "HLJXC_AccountAoucher");
		}
		public bool Exists(int AccountAoucherID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HLJXC_AccountAoucher");
			stringBuilder.Append(" where AccountAoucherID=@AccountAoucherID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountAoucherID", SqlDbType.Int, 4)
			};
			array[0].Value = AccountAoucherID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.AccountAoucher model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLJXC_AccountAoucher(");
			stringBuilder.Append("StockOrderID,AccountANumber,AccountACount,AccountAExplain,Inputer,IsCaudit,CauditName,IsAccount,AccountName,IsCashier,CashierName,InputDate,UpdateDate,CauditDate,AccountDate,CashierDate,OwnerID)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@StockOrderID,@AccountANumber,@AccountACount,@AccountAExplain,@Inputer,@IsCaudit,@CauditName,@IsAccount,@AccountName,@IsCashier,@CashierName,@InputDate,@UpdateDate,@CauditDate,@AccountDate,@CashierDate,@OwnerID)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@StockOrderID", SqlDbType.Int, 4),
				new SqlParameter("@AccountANumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountACount", SqlDbType.Int, 4),
				new SqlParameter("@AccountAExplain", SqlDbType.NVarChar, 255),
				new SqlParameter("@Inputer", SqlDbType.NVarChar, 50),
				new SqlParameter("@IsCaudit", SqlDbType.Bit, 1),
				new SqlParameter("@CauditName", SqlDbType.NVarChar, 50),
				new SqlParameter("@IsAccount", SqlDbType.Bit, 1),
				new SqlParameter("@AccountName", SqlDbType.NVarChar, 50),
				new SqlParameter("@IsCashier", SqlDbType.Bit, 1),
				new SqlParameter("@CashierName", SqlDbType.NVarChar, 50),
				new SqlParameter("@InputDate", SqlDbType.DateTime),
				new SqlParameter("@UpdateDate", SqlDbType.DateTime),
				new SqlParameter("@CauditDate", SqlDbType.DateTime),
				new SqlParameter("@AccountDate", SqlDbType.DateTime),
				new SqlParameter("@CashierDate", SqlDbType.DateTime),
				new SqlParameter("@OwnerID", SqlDbType.Int, 4)
			};
			array[0].Value = model.StockOrderID;
			array[1].Value = model.AccountANumber;
			array[2].Value = model.AccountACount;
			array[3].Value = model.AccountAExplain;
			array[4].Value = model.Inputer;
			array[5].Value = model.IsCaudit;
			array[6].Value = model.CauditName;
			array[7].Value = model.IsAccount;
			array[8].Value = model.AccountName;
			array[9].Value = model.IsCashier;
			array[10].Value = model.CashierName;
			array[11].Value = model.InputDate;
			array[12].Value = model.UpdateDate;
			array[13].Value = model.CauditDate;
			array[14].Value = model.AccountDate;
			array[15].Value = model.CashierDate;
			array[16].Value = model.OwnerID;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public int AddTransaction(HKB.Data.Model.AccountAoucher model, Conn conn)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLJXC_AccountAoucher(");
			stringBuilder.Append("StockOrderID,AccountANumber,AccountACount,AccountAExplain,Inputer,IsCaudit,CauditName,IsAccount,AccountName,IsCashier,CashierName,InputDate,UpdateDate,CauditDate,AccountDate,CashierDate,OwnerID)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@StockOrderID,@AccountANumber,@AccountACount,@AccountAExplain,@Inputer,@IsCaudit,@CauditName,@IsAccount,@AccountName,@IsCashier,@CashierName,@InputDate,@UpdateDate,@CauditDate,@AccountDate,@CashierDate,@OwnerID)");
			stringBuilder.Append("  select SCOPE_IDENTITY()");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@StockOrderID", SqlDbType.Int, 4),
				new SqlParameter("@AccountANumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountACount", SqlDbType.Int, 4),
				new SqlParameter("@AccountAExplain", SqlDbType.NVarChar, 255),
				new SqlParameter("@Inputer", SqlDbType.NVarChar, 50),
				new SqlParameter("@IsCaudit", SqlDbType.Bit, 1),
				new SqlParameter("@CauditName", SqlDbType.NVarChar, 50),
				new SqlParameter("@IsAccount", SqlDbType.Bit, 1),
				new SqlParameter("@AccountName", SqlDbType.NVarChar, 50),
				new SqlParameter("@IsCashier", SqlDbType.Bit, 1),
				new SqlParameter("@CashierName", SqlDbType.NVarChar, 50),
				new SqlParameter("@InputDate", SqlDbType.DateTime),
				new SqlParameter("@UpdateDate", SqlDbType.DateTime),
				new SqlParameter("@CauditDate", SqlDbType.DateTime),
				new SqlParameter("@AccountDate", SqlDbType.DateTime),
				new SqlParameter("@CashierDate", SqlDbType.DateTime),
				new SqlParameter("@OwnerID", SqlDbType.Int, 4)
			};
			array[0].Value = model.StockOrderID;
			array[1].Value = model.AccountANumber;
			array[2].Value = model.AccountACount;
			array[3].Value = model.AccountAExplain;
			array[4].Value = model.Inputer;
			array[5].Value = model.IsCaudit;
			array[6].Value = model.CauditName;
			array[7].Value = model.IsAccount;
			array[8].Value = model.AccountName;
			array[9].Value = model.IsCashier;
			array[10].Value = model.CashierName;
			array[11].Value = model.InputDate;
			array[12].Value = model.UpdateDate;
			array[13].Value = model.CauditDate;
			array[14].Value = model.AccountDate;
			array[15].Value = model.CashierDate;
			array[16].Value = model.OwnerID;
			object obj = conn.ExecuteTransactionScalar(stringBuilder.ToString(), array);
			int result;
			if (obj == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(obj);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.AccountAoucher model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HLJXC_AccountAoucher set ");
			stringBuilder.Append("StockOrderID=@StockOrderID,");
			stringBuilder.Append("AccountANumber=@AccountANumber,");
			stringBuilder.Append("AccountACount=@AccountACount,");
			stringBuilder.Append("AccountAExplain=@AccountAExplain,");
			stringBuilder.Append("Inputer=@Inputer,");
			stringBuilder.Append("IsCaudit=@IsCaudit,");
			stringBuilder.Append("CauditName=@CauditName,");
			stringBuilder.Append("IsAccount=@IsAccount,");
			stringBuilder.Append("AccountName=@AccountName,");
			stringBuilder.Append("IsCashier=@IsCashier,");
			stringBuilder.Append("CashierName=@CashierName,");
			stringBuilder.Append("InputDate=@InputDate,");
			stringBuilder.Append("UpdateDate=@UpdateDate,");
			stringBuilder.Append("CauditDate=@CauditDate,");
			stringBuilder.Append("AccountDate=@AccountDate,");
			if (!string.IsNullOrEmpty(model.OwnerID.ToString()))
			{
				stringBuilder.Append("OwnerID=" + model.OwnerID + ",");
			}
			stringBuilder.Append("CashierDate=@CashierDate");
			stringBuilder.Append(" where AccountAoucherID=@AccountAoucherID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@StockOrderID", SqlDbType.Int, 4),
				new SqlParameter("@AccountANumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountACount", SqlDbType.Int, 4),
				new SqlParameter("@AccountAExplain", SqlDbType.NVarChar, 255),
				new SqlParameter("@Inputer", SqlDbType.NVarChar, 50),
				new SqlParameter("@IsCaudit", SqlDbType.Bit, 1),
				new SqlParameter("@CauditName", SqlDbType.NVarChar, 50),
				new SqlParameter("@IsAccount", SqlDbType.Bit, 1),
				new SqlParameter("@AccountName", SqlDbType.NVarChar, 50),
				new SqlParameter("@IsCashier", SqlDbType.Bit, 1),
				new SqlParameter("@CashierName", SqlDbType.NVarChar, 50),
				new SqlParameter("@InputDate", SqlDbType.DateTime),
				new SqlParameter("@UpdateDate", SqlDbType.DateTime),
				new SqlParameter("@CauditDate", SqlDbType.DateTime),
				new SqlParameter("@AccountDate", SqlDbType.DateTime),
				new SqlParameter("@CashierDate", SqlDbType.DateTime),
				new SqlParameter("@AccountAoucherID", SqlDbType.Int, 4)
			};
			array[0].Value = model.StockOrderID;
			array[1].Value = model.AccountANumber;
			array[2].Value = model.AccountACount;
			array[3].Value = model.AccountAExplain;
			array[4].Value = model.Inputer;
			array[5].Value = model.IsCaudit;
			array[6].Value = model.CauditName;
			array[7].Value = model.IsAccount;
			array[8].Value = model.AccountName;
			array[9].Value = model.IsCashier;
			array[10].Value = model.CashierName;
			array[11].Value = model.InputDate;
			array[12].Value = model.UpdateDate;
			array[13].Value = model.CauditDate;
			array[14].Value = model.AccountDate;
			array[15].Value = model.CashierDate;
			array[16].Value = model.AccountAoucherID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int AccountAoucherID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_AccountAoucher ");
			stringBuilder.Append(" where AccountAoucherID=@AccountAoucherID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountAoucherID", SqlDbType.Int, 4)
			};
			array[0].Value = AccountAoucherID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string AccountAoucherIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_AccountAoucher ");
			stringBuilder.Append(" where AccountAoucherID in (" + AccountAoucherIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.AccountAoucher GetModel(int AccountAoucherID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HLJXC_AccountAoucher ");
			stringBuilder.Append(" where AccountAoucherID=@AccountAoucherID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountAoucherID", SqlDbType.Int, 4)
			};
			array[0].Value = AccountAoucherID;
			HKB.Data.Model.AccountAoucher accountAoucher = new HKB.Data.Model.AccountAoucher();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.AccountAoucher result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.AccountAoucher DataRowToModel(DataRow row)
		{
			HKB.Data.Model.AccountAoucher accountAoucher = new HKB.Data.Model.AccountAoucher();
			if (row != null)
			{
				if (row["AccountAoucherID"] != null && row["AccountAoucherID"].ToString() != "")
				{
					accountAoucher.AccountAoucherID = int.Parse(row["AccountAoucherID"].ToString());
				}
				if (row["StockOrderID"] != null && row["StockOrderID"].ToString() != "")
				{
					accountAoucher.StockOrderID = new int?(int.Parse(row["StockOrderID"].ToString()));
				}
				if (row["AccountANumber"] != null)
				{
					accountAoucher.AccountANumber = row["AccountANumber"].ToString();
				}
				if (row["AccountACount"] != null && row["AccountACount"].ToString() != "")
				{
					accountAoucher.AccountACount = new int?(int.Parse(row["AccountACount"].ToString()));
				}
				if (row["AccountAExplain"] != null)
				{
					accountAoucher.AccountAExplain = row["AccountAExplain"].ToString();
				}
				if (row["Inputer"] != null)
				{
					accountAoucher.Inputer = row["Inputer"].ToString();
				}
				if (row["IsCaudit"] != null && row["IsCaudit"].ToString() != "")
				{
					if (row["IsCaudit"].ToString() == "1" || row["IsCaudit"].ToString().ToLower() == "true")
					{
						accountAoucher.IsCaudit = true;
					}
					else
					{
						accountAoucher.IsCaudit = false;
					}
				}
				if (row["CauditName"] != null)
				{
					accountAoucher.CauditName = row["CauditName"].ToString();
				}
				if (row["IsAccount"] != null && row["IsAccount"].ToString() != "")
				{
					if (row["IsAccount"].ToString() == "1" || row["IsAccount"].ToString().ToLower() == "true")
					{
						accountAoucher.IsAccount = true;
					}
					else
					{
						accountAoucher.IsAccount = false;
					}
				}
				if (row["AccountName"] != null)
				{
					accountAoucher.AccountName = row["AccountName"].ToString();
				}
				if (row["IsCashier"] != null && row["IsCashier"].ToString() != "")
				{
					if (row["IsCashier"].ToString() == "1" || row["IsCashier"].ToString().ToLower() == "true")
					{
						accountAoucher.IsCashier = true;
					}
					else
					{
						accountAoucher.IsCashier = false;
					}
				}
				if (row["CashierName"] != null)
				{
					accountAoucher.CashierName = row["CashierName"].ToString();
				}
				if (row["InputDate"] != null && row["InputDate"].ToString() != "")
				{
					accountAoucher.InputDate = new System.DateTime?(System.DateTime.Parse(row["InputDate"].ToString()));
				}
				if (row["UpdateDate"] != null && row["UpdateDate"].ToString() != "")
				{
					accountAoucher.UpdateDate = new System.DateTime?(System.DateTime.Parse(row["UpdateDate"].ToString()));
				}
				if (row["CauditDate"] != null && row["CauditDate"].ToString() != "")
				{
					accountAoucher.CauditDate = new System.DateTime?(System.DateTime.Parse(row["CauditDate"].ToString()));
				}
				if (row["AccountDate"] != null && row["AccountDate"].ToString() != "")
				{
					accountAoucher.AccountDate = new System.DateTime?(System.DateTime.Parse(row["AccountDate"].ToString()));
				}
				if (row["CashierDate"] != null && row["CashierDate"].ToString() != "")
				{
					accountAoucher.CashierDate = new System.DateTime?(System.DateTime.Parse(row["CashierDate"].ToString()));
				}
				if (row["OwnerID"] != System.DBNull.Value)
				{
					accountAoucher.OwnerID = System.Convert.ToInt32(row["OwnerID"].ToString());
				}
			}
			return accountAoucher;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * FROM HLJXC_AccountAoucher ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HLJXC_AccountAoucher ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HLJXC_AccountAoucher ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.AccountAoucherID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HLJXC_AccountAoucher T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataTable GetListAccountAoucherItemSubjcetByAccountAoucherID(int accountAoucherID)
		{
			string text = "select * from [HLJXC_View_AccountAoucherItemSubjcet] where [AccountAoucherID]=" + accountAoucherID;
			return Conn.Query(text.ToString(), new IDbDataParameter[0]).Tables[0];
		}
		public DataTable GetListAccountAoucherItemSubjcet(string strSQLWhere)
		{
			string text = "select * from [HLJXC_View_AccountAoucherItemSubjcet]" + strSQLWhere + " order by [AccountAoucherID] asc";
			return Conn.Query(text.ToString(), new IDbDataParameter[0]).Tables[0];
		}
		public DataTable GetListAccountAoucherItemSubjcetContainOwnerName(string strSQLWhere)
		{
			string text = "select (select UserName from HL_User where UserID=OwnerID) as OwnerName,* from [HLJXC_View_AccountAoucherItemSubjcet]" + strSQLWhere + " order by [AccountAoucherID] asc";
			return Conn.Query(text.ToString(), new IDbDataParameter[0]).Tables[0];
		}
		public int GetListAccountAoucherItemSubjcetRecordCount(string strSQLWhere)
		{
			Conn conn = new Conn();
			string sql = "select COUNT(*) from [HLJXC_View_AccountAoucherItemSubjcet] " + strSQLWhere;
			return conn.ExecuteCount(sql);
		}
		public DataTable GetListAccountAoucherItemSubjcetPage(string strSQLWhere, string OrderBy, int startIndex, int endIndex)
		{
			if (string.IsNullOrEmpty(OrderBy))
			{
				OrderBy = "order by [AccountAoucherID] desc";
			}
			string text = "select ROW_NUMBER() OVER (" + OrderBy + ") as row,* from [HLJXC_View_AccountAoucherItemSubjcet] " + strSQLWhere;
			string text2 = string.Concat(new object[]
			{
				"SELECT * FROM ( ",
				text,
				" ) as TT WHERE TT.row between ",
				startIndex,
				" and ",
				endIndex
			});
			return Conn.Query(text2.ToString(), new IDbDataParameter[0]).Tables[0];
		}
	}
}
